package com.ncs.leisure.beans.business.impl;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.BeanUtils;

import com.ncs.leisure.beans.business.BeansContactBusiness;
import com.ncs.leisure.beans.business.dao.BeansMapTemplateDAO;
import com.ncs.leisure.beans.business.dao.BeansMemberDAO;
import com.ncs.leisure.beans.business.dao.po.*;
import com.ncs.leisure.beans.ui.vo.BeansMemberVo;
import com.ncs.leisure.beans.ui.vo.Map;

public class BeansContactBusinessImpl implements BeansContactBusiness{
	
	BeansMapTemplateDAO beansMapTemplateDAO;
	
	BeansMemberDAO beansMemberDAO;
	
	public BeansMemberDAO getBeansMemberDAO() {
		return beansMemberDAO;
	}

	public void setBeansMemberDAO(BeansMemberDAO beansMemberDAO) {
		this.beansMemberDAO = beansMemberDAO;
	}

	public BeansMapTemplateDAO getBeansMapTemplateDAO() {
		return beansMapTemplateDAO;
	}

	public void setBeansMapTemplateDAO(BeansMapTemplateDAO beansMapTemplateDAO) {
		this.beansMapTemplateDAO = beansMapTemplateDAO;
	}
	
	/**  bussiness **/

	public Object findMapList(){
		
		List<BeansMapTemplate> beansMapTemplateList=beansMapTemplateDAO.findAll();
		
		List<Map> mapVoList=new ArrayList<Map>();
		
		Map map=null;
		
		BeansMapTemplate bmt=null;
		
		String[] mapTemp=null;
		
		for(int i=0;i<beansMapTemplateList.size();i++){
			
			map=new Map();
			
			bmt=beansMapTemplateList.get(i);
			
			map.setId(bmt.getId());
			
			map.setName(bmt.getName());
			
			map.setPersonnum(bmt.getPersonnum());
			
			map.setDiffcult(bmt.getDiffcult());
			
			map.setImg(bmt.getImg());
			
			mapTemp=bmt.getMapbase().split(",");
			
			map.setWidth(Integer.parseInt(mapTemp[0]));
			
			map.setHeight(Integer.parseInt(mapTemp[1]));
			
			map.setMapBody(mapTemp[2]);
			
			mapVoList.add(map);
			
		}
		
		return mapVoList;
	}
	
	public Object findMapMirrorById(Integer mapId){
		
		List<BeansMapTemplate> beansMapTemplateList=beansMapTemplateDAO.findById(mapId);
		
		String[] mapTemp=null;
		
		if(beansMapTemplateList.size()==0){
			
			return "error:null";
			
		}else if(beansMapTemplateList.size()>1){
			
			return "error:more";
			
		}else{
			
			Map map=new Map();
			
			BeansMapTemplate bmt=beansMapTemplateList.get(0);
			
			bmt=beansMapTemplateList.get(0);
			
			map.setName(bmt.getName());
			
			map.setPersonnum(bmt.getPersonnum());
			
			map.setDiffcult(bmt.getDiffcult());
			
			map.setImg(bmt.getImg());
			
			mapTemp=bmt.getMapbase().split(",");
			
			map.setWidth(Integer.parseInt(mapTemp[0]));
			
			map.setHeight(Integer.parseInt(mapTemp[1]));
			
			map.setMapBody(mapTemp[2]);
			
			return map;
			
		}
		
	}

	public Object findBeansMemberVo(Integer memberId) {
		
		List<BeansMember> beansMemberList = beansMemberDAO.findByMemberId(memberId);
		
		if(beansMemberList.size()==0){
			
			return "error:null";
			
		}else if(beansMemberList.size()>1){
			
			return "error:more";
			
		}else{
			
			BeansMemberVo beansMemberVo=new BeansMemberVo();
			
			BeansMember bm=beansMemberList.get(0);
			
			BeanUtils.copyProperties(bm,beansMemberVo);
			
			return beansMemberVo;
			
		}
		
	}

}
